<?php

class MeterRecordModel extends CommonModel {
	protected $tableName = 'meter_record';
	protected $_validate = array(
             
		);
                	protected $_auto	 =	 array(
		            
		array('inputdate','time',self::MODEL_INSERT,'function'),
		);
	
                      //添加表数据
                      function addMeterItem($data){
                             
                              $result = $this->db->execute('INSERT INTO '.$this->tablePrefix.'meter_item ( meterid, bizid, meterNum1, meterNum2,meterprice, recordid,last_date) '
                                                .'VALUES ('.$data['meterid'].','.$data['bizid'].','.$data['meterNum1'].','.$data['meterNum2'].','.$data['meterprice'].','.$data['recordid'].','.$data['last_update'].') ');
                          
                            
                             if($result===false) {
                                    return false;
                            }else {
                               //更新表biz_meter last_update 最后更新时间信息 .last_num 最后抄表表数据
                              $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'biz_meter SET last_update='.$data['last_update'].' , last_num='.$data['meterNum2'].'  WHERE id='.$data['meterid'].' AND bizid='.$data['bizid']); 

                              return true;
                           }
                     
                          
                      }
                      //添加表数据
                      function updateMeterItem($data){

                              $result = $this->db->execute('UPDATE '.$this->tablePrefix.'meter_item SET  meterNum1='.$data['meterNum1'].', meterNum2='.$data['meterNum2'].', meterprice='.$data['meterprice'].'   WHERE '
                                        .' recordid= '.$data['recordid'].'  AND meterid='.$data['meterid'].' AND bizid='.$data['bizid']);
                          
                            
                             if($result===false) {
                                    return false;
                            }else {
                              
                                    return true;
                           }
                     
                          
                      }                      
                      //更新最后月份信息
                      function updateLastMon($bizid,$montime){
                          //更新最后月份信息
                          $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'biz SET lastdate='.$montime.'  WHERE id='.$bizid);
                          if($rs===false) {
                                    return false;
                           }else {


                              return true;
                           }
                          
                      }
                       function setCount($zoneid,$mon,$untype){
                              $rs= $this->db->query('select id  FROM  '.$this->tablePrefix.'record_statistics  WHERE  id='.$zoneid.' AND mondate='.$mon);
                              if(sizeof($rs)>0){
                                //Update  
                                if($untype==1){
                                    $Unfinished=',Unfinished=Unfinished+1';
                                }
                                $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'record_statistics SET inputNum=inputNum+1 '.$Unfinished.'  WHERE  id='.$zoneid.' AND mondate='.$mon);
                                if($rs===false) {
                                    return false;
                                }else {
                                    return true;
                                } 
                              }else{
                                //insert
                                  $un=0;
                                  if($untype==1){
                                      $un=1;
                                  }
                                  $rs = $this->db->execute('INSERT INTO '.$this->tablePrefix.'record_statistics (zoneid,mondate,paynum,inputNum,Unfinished,invoNum) '
                                                .'VALUES ('.$zoneid.','.$mon.',0,1,'.$un.',0) ');
                          
                                if($rs===false) {
                                    return false;
                                }else {
                                    return true;
                                } 
                              }
                              
                       }
                       function setMeterStatistics($bizid,$metertype,$year,$mon,$mNum){
                            $rs = $this->db->execute('UPDATE '.$this->tablePrefix.'meter_statistics SET mon'.$mon.'='.$mNum.'  WHERE   metertype='.$metertype.' AND bizid='.$bizid.' AND year='.$year);
                                if($rs===false) {
                                    return false;
                                }else {
                                    return true;
                              } 
                           
                       }
                       function setMeterStatisticsCache($year){
                           $rs = $this->db->query('select  * FROM  '.$this->tablePrefix.'meter_statistics');
                           if($rs===false) {
                                    return false;
                                }else {
                                     F($year.'_stat',Null);
                                     F($year.'_stat',$rs);


                                    return true;
                           } 
                       }

}
?>
